/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pl.put.dcs_modeler.project.projectPropertiesExtensions.dcsProjectProperties;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import javax.swing.JComponent;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectUtils;
import org.netbeans.spi.project.AuxiliaryConfiguration;
import org.netbeans.spi.project.ui.support.ProjectCustomizer;
import org.netbeans.spi.project.ui.support.ProjectCustomizer.Category;
import org.openide.util.Lookup;
import org.openide.xml.XMLUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import pl.put.dcs_modeler.project.projectPropertiesExtensions.dcsProjectProperties.projectConfigModel.DCSServerConfig;
import pl.put.dcs_modeler.utils.DCSProjectUtil;
/**
 *
 * @author LukaszR
 */
public class DCSProjectPropertiesProvider implements ProjectCustomizer.CompositeCategoryProvider
{
    public static Logger log = Logger.getLogger( DCSProjectPropertiesProvider.class.getName() );

    private DCSProjectPropertiesPanel panel;
    private String serverPath;

    public static DCSProjectPropertiesProvider createDCSProjectPropertiesPanel()
    {
        return new DCSProjectPropertiesProvider();
    }

    @Override
    public Category createCategory( Lookup lookup )
    {
        final Project p = lookup.lookup( Project.class );
        

        ProjectCustomizer.Category ret = null;
        // tylko, jezeli jest to projekt dcs
        if ( DCSProjectUtil.isDCSProject( p ) )
        {
            ret = ProjectCustomizer.Category.create( org.openide.util.NbBundle.getMessage(DCSProjectPropertiesProvider.class, "DCS_Project_Properties"), org.openide.util.NbBundle.getMessage(DCSProjectPropertiesProvider.class, "DCS_Project_Properties_Desc"), null, null );
            ret.setStoreListener( new ActionListener()
                                        {
                                            @Override
                                            public void actionPerformed( ActionEvent e )
                                            {
                                                storeSettings( p );
                                            }
                                        });

            ret.setOkButtonListener( new ActionListener()
                                        {
                                            @Override
                                            public void actionPerformed( ActionEvent e )
                                            {
                                                storeSettings( p );
                                            }
                                        });
        }
        return ret;
    }

    @Override
    public JComponent createComponent( Category cat, Lookup lookup )
    {
        Project p = lookup.lookup( Project.class );
        readSettigs( p );
        panel = new DCSProjectPropertiesPanel( serverPath );
        return panel;
    }

    private void readSettigs( Project p )
    {
        serverPath = DCSProjectUtil.getServersConfig( p ).get( 0 ).getServerPath();

    }

    public void setServerPath( String path )
    {
        serverPath = path;
        panel.setServerPath( path );
    }

    public void storeSettings( Project p )
    {
        serverPath = panel.getServerPath();
        DCSServerConfig server = new DCSServerConfig();
        server.setServerPath( serverPath );
        List< DCSServerConfig > servers = new ArrayList< DCSServerConfig >();
        servers.add( server );
        DCSProjectUtil.setServersConfig( p, servers );
    }

}
